package com.yj.service.impl;

import com.yj.entity.Details;
import com.yj.entity.Houses;
import com.yj.dao.DetailsMapper;
import com.yj.dao.HousesMapper;
import com.yj.dao.UsersMapper;
import com.yj.service.IDetailsService;
import com.yj.util.PageUtil;
import com.yj.vo.JsonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *订单表
 * @author yj
 * @since 2020-08-24
 */
@Service
public class DetailsServiceImpl extends ServiceImpl<DetailsMapper, Details> implements IDetailsService {

	@Autowired
	DetailsMapper details;
	
	@Autowired
	HousesMapper houses;
	
	@Autowired
	UsersMapper users;
	
	@Override
	public JsonResult finAllDetail(int puid) {
		JsonResult ret = null;
		List<Map<String, Object>> detail_houseList = new ArrayList<Map<String,Object>>();
		
		QueryWrapper<Details> wrapper = new QueryWrapper<Details>();
//		wrapper.apply("details.uxid in (select ud.uxid from user2details ud where ud.uid="+uid+")");
		wrapper.eq("puid", puid);
		List<Details> list = details.selectList(wrapper);//查出该用户的详细订单
		if(list!=null) {
		for(Details de : list) {
			Houses house = houses.selectById(de.getHid());
			if(house!=null) {
				String uname = users.selectById(house.getUid()).getUname();
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("housename", uname);
				map.put("house", house);
				map.put("detail", de);
				detail_houseList.add(map);
			}
		}
			ret = new JsonResult(true, "查询到所有订单详情");
			ret.setData("details", detail_houseList);
			return ret;
		}else {
			ret = new JsonResult(false, "该用户未查到订单");
			return ret;
		}
	}

	@Override
	public JsonResult addDetail(Details detail) {
		JsonResult ret = null;
		UpdateWrapper<Houses> updateWrapper = new UpdateWrapper<Houses>();
		
		int n = details.insert(detail);
		if(n!=0) {
			updateWrapper.set("state", "已售");
			updateWrapper.eq("hid", detail.getHid());
			int update = houses.update(null,updateWrapper);
			if(update!=0) {
				ret = new JsonResult(true, "订单新增了");
				ret.setData("info", "订单生成成功");
				return ret;
			}else {
				ret = new JsonResult(false, "订单生成失败");
				return ret;
			}
		}else {
			ret = new JsonResult(false, "订单保存失败");
			return ret;
		}
	}

	@Override
	public JsonResult delDetail(int xid) {
		JsonResult jsonResult=new JsonResult(false, null);
		Details detai=details.selectById(xid);
		Houses h=houses.selectById(detai.getHid());
		h.setState("未售");
		int n=houses.updateById(h);
		int num=details.deleteById(xid);
		
		
		if (num>0&&n>0) {
			jsonResult.setMessage("删除订单成功");
		}else {
			jsonResult.setMessage("删除订单失败");
		}
		return jsonResult;
	}

	@Override
	public JsonResult pages() {
		
		return PageUtil.pages(details, null, 6);
	}

	@Override
	public JsonResult page(int pageNum) {
		JsonResult ret = null;
		PageInfo pageInfo = PageUtil.test(pageNum, 6, details, null);
		List<Details> list = pageInfo.getList();
		List resultList=new ArrayList<>();
		for (Details d:list) {
			
		HashMap map=new HashMap();	
		String	uname=users.selectById(d.getPuid()).getUname();
		map.put("uname", uname);
		map.put("detail", d);
		resultList.add(map);
		}
		if (list != null && pageInfo.getNavigatepageNums().length != 0) {
			ret = new JsonResult(true, "当前页数据查询成功");
			ret.setData("page", resultList);
			return ret;
		} else {
			ret = new JsonResult(false, "当前页查询失败");
			return ret;
		}
	}
	

	
	
}
